package repository

import (
	"context"
	"gitee.com/xiao_hange/go-admin-pkg/pkg/tool"
	"go-admin/server/internal/domain"
	"go-admin/server/internal/repository/dao"
)

type PackRepository interface {
	Create(ctx context.Context, pack domain.Pack) error
	List(ctx context.Context, page, limit int) ([]domain.Pack, int, error)
}

type packRepository struct {
	dao dao.PackDao
}

func NewPackRepository(dao dao.PackDao) PackRepository {
	return &packRepository{
		dao: dao,
	}
}

func (p *packRepository) Create(ctx context.Context, pack domain.Pack) error {
	var daoPack dao.Pack
	tool.ConvertDTOToDAO(pack, &daoPack)
	return p.dao.Insert(ctx, daoPack)
}
func (p *packRepository) List(ctx context.Context, page, limit int) ([]domain.Pack, int, error) {
	lists, total, err := p.dao.List(ctx, page, limit)
	result := make([]domain.Pack, len(lists))
	for i, list := range lists {
		var dtoPack domain.Pack
		tool.ConvertDAOToDTO(list, &dtoPack)
		result[i] = dtoPack
	}
	return result, total, err
}
